package com.nm.web.controller;

import com.nm.commons.exception.SupperException;
import com.nm.orm.pojo.ArticleEntity;
import com.nm.orm.pojo.out.ArticleBackPageOutput;
import com.nm.orm.pojo.out.ArticleOutput;
import com.nm.service.ArticleService;
import com.nm.service.page.PageEntity;
import com.nm.web.convert.ArticleConvertor;
import com.nm.web.request.ArticleBackPageRequest;
import com.nm.web.request.ArticlePageRequest;
import com.nm.web.request.ArticleUpdateRequest;
import com.nm.web.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(path = "/article")
public class ArticleController extends BaseController {

    @Autowired
    private ArticleService arcSvc;

    /**
     * 文章主view
     */
    @RequestMapping(path = "")
    public ModelAndView index() {
        ModelAndView model = new ModelAndView();
        model.setViewName("/admin/article/index");
        return model;
    }

    /**
     * 前台分页查询文章
     *
     * @param request
     * @return
     */
    @RequestMapping("/page")
    @ResponseBody
    public ResponseEntity<Object> page(@RequestBody ArticlePageRequest request) {

        PageEntity<ArticleOutput> page = arcSvc.pageQuery(ArticleConvertor.conv(request));

        return getResult(page);
    }

    /**
     * 后台分页查询所有文章
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/backPage")
    @ResponseBody
    public ResponseEntity<Object> backPage(@RequestBody ArticleBackPageRequest request) {

        PageEntity<ArticleBackPageOutput> page = arcSvc.backPageQuery(ArticleConvertor.conv(request));

        return getResult(page);
    }

//    /**
//     * 查询所有文章
//     *
//     * @return
//     */
//    @RequestMapping(path = "/all", method = RequestMethod.POST)
//    @ResponseBody
//    public ResponseEntity<List<ArticleEntity>> findAll()
//            throws SupperException {
//
//        List<ArticleEntity> output = arcSvc.findAll();
//
//        return getResult(output);
//    }

    /**
     * 增加文章视图
     */
    @RequestMapping(path = "/add", method = RequestMethod.GET)
    public ModelAndView add() throws SupperException {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/admin/article/info");
        modelAndView.addObject("title", "增加文章");
        return modelAndView;
    }

    /**
     * 增加修改文章数据提交
     */
    @RequestMapping(path = "/datapost")
    public ResponseEntity<Result> dataPost(@RequestParam ArticleUpdateRequest article)
            throws SupperException {

        arcSvc.update(ArticleConvertor.conv(article));

        return getResult(null);
    }

    /**
     * 查新更新对象 展示修改视图
     *
     * @param id 文章ID
     * @return
     */
    @RequestMapping(path = "/update/{id}", method = RequestMethod.GET)
    public ModelAndView update(@PathVariable("id") String id) throws SupperException {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/admin/article/info");

        ArticleEntity article = arcSvc.getById(id);

        modelAndView.addObject("title", "修改文章");
        modelAndView.addObject("article", new Result(null));
        return modelAndView;
    }


    /**
     * 删除数据
     *
     * @param id 文章ID
     * @return
     */
    @RequestMapping(path = "/delete/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ResponseEntity<Result> delete(@PathVariable("id") String id) throws SupperException {

        arcSvc.deleteById(id);

        return getResult(null);
    }

}
